package org.huangrui.javase.basic;

/**
 * @Author hr
 * @Create 2025-11-06 19:45
 **/
public class T01AndOr {
    public static void main(String[] args) {
        or();
        // and();
    }

    /**
     * & 按位与
     * && 短路与
     */
    private static void and() {
        System.out.println(9 & 5); //结果是1
         /*
        9的二进制：00000000 00000000 00000000 00001001
        5的二进制：00000000 00000000 00000000 00000101
        9&5的：   00000000 00000000 00000000 00000001（十进制1）
        */
        int x = 1, y = 1;
        System.out.println(x++ == 2 & ++y == 2);//false & true结果false
        //取x的1,x自增为2,  1==2 false，  y自增为2，取y的值2，2==2 true
        System.out.println("x = " + x + ", y = " + y);//x=2,y=2

        System.out.println(++x == 2 && ++y == 2);//结果false
        //x自增为3,取x的3,  3==2 false， 短路
        System.out.println("x = " + x + ", y = " + y);//x=3,y=2
    }


    /**
     * |  按位或
     * || 短路或
     */
    private static void or() {
        int x = 1, y = 1;
        System.out.println(9 | 5);//结果13
        /*
        9的二进制：00000000 00000000 00000000 00001001
        5的二进制：00000000 00000000 00000000 00000101
        9|5的：   00000000 00000000 00000000 00001101（十进制13）
        */
        System.out.println(x++ == 1 | ++y == 1);//true | false 结果是true
        //取x的1,x自增为2,  1==1 true，  y自增为2,取y的值2, 2==1 false
        System.out.println("x = " + x + ", y = " + y);//x=2,y=2

        System.out.println(x++ == 2 || ++y == 2);//true
        //取x的2,x自增为3,  2==2 true， 短路
        System.out.println("x = " + x + ", y = " + y);//x=3,y=2
    }
}
